#!/bin/bash
# author tyb

#函数
function isgreen() {
    if [ $? -eq 0 ]; then
        echo -e "\e[32m$1==========>succeed\e[0m"
    else
        echo -e "\e[31m$1==========>failed\e[0m"
    fi
}
function network() {
    #超时时间
    local timeout=1

    #目标网站
    local target=www.baidu.com

    #获取响应状态码
    local ret_code
    ret_code=$(curl -I -s --connect-timeout ${timeout} ${target} -w "%{http_code}" | tail -n1)

    if [ "$ret_code" = "200" ]; then
        #网络畅通
        echo -e "\e[32m网络畅通==========>succeed\e[0m"
    else
        #网络不畅通
        echo -e "\e[31m网络不畅通,请检查网络==========>succeed\e[0m"
        exit 1
    fi
}

#全局变量
hostname=master
ip=$(ip addr | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | awk -F "/" '{print $1}')
image_repository="registry.aliyuncs.com/google_containers"
pod_network="10.244.0.0/16"

echo "初始化操作"
#修改主机名
hostnamectl set-hostname "$hostname"
isgreen 修改主机名

#配置hosts解析文件
if grep -q "$hostname" /etc/hosts; then
    #解析存在进行修改
    sed -i "s/.*$hostname.*/$ip $hostname/g" /etc/hosts
    isgreen 配置hosts文件
else
    echo "$ip $hostname" >>/etc/hosts
    isgreen 配置hosts文件
fi

#关闭防火墙
echo "关闭防火墙"
systemctl disable firewalld --now >/dev/null 2>&1
isgreen "关闭防火墙"

#关闭selinux
echo "关闭selinux"
setenforce 0
isgreen "临时关闭"
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
isgreen "永久关闭"

#优化ssh连接
echo "优化ssh连接"
sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
systemctl restart sshd
isgreen 优化ssh连接

#修改内核参数
echo "修改内核参数"
modprobe br_netfilter
if ! grep -q modprobe /etc/profile; then
    echo "modprobe br_netfilter" >>/etc/profile
fi
isgreen 加载内核模块

if [ ! -f "/etc/sysctl.d/k8s.conf" ]; then
    cat >/etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
fi
sysctl -p /etc/sysctl.d/k8s.conf >/dev/null 2>&1
isgreen 修改内核参数

#配置yum源
echo "配置yum源"
network
if [ ! -f "/etc/yum.repos.d/docker.repo" ]; then
    rm -rf /etc/yum.repos.d/*
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo >/dev/null 2>&1
    isgreen "aliyun-base源"
    curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo >/dev/null 2>&1
    isgreen "aliyun-epel源"
    curl -o /etc/yum.repos.d/docker.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo >/dev/null 2>&1
    isgreen "aliyun-docker源"
    cat >/etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF
    isgreen "aliyun-k8s源"
else
    echo -e "\e[32m配置yum源==========>succeed\e[0m"
fi

#开启ipvs
echo "开启ipvs"
create_ipvs_modules() {
    cat >/etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in ${ipvs_modules}; do
 /sbin/modinfo -F filename ${kernel_module} > /dev/null 2>&1
 if [ 0 -eq 0 ]; then
 /sbin/modprobe ${kernel_module}
 fi
done
EOF

}

if [ ! -f "/etc/sysconfig/modules/ipvs.modules" ]; then
    create_ipvs_modules
    chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep ip_vs >/dev/null 2>&1
    isgreen "开启ipvs"
else
    lsmod | grep ip_vs >/dev/null 2>&1
    isgreen "开启ipvs"
fi

#安装常用软件
echo "install vim wget bash-completion net-tools"
yum install -y vim wget bash-completion net-tools >/dev/null 2>&1
isgreen "install pkgs"

#配置时间同步
echo "配置时间同步"
if ! rpm -q ntpdate >/dev/null 2>&1; then
    yum install -y ntpdate >/dev/null 2>&1
    isgreen "install ntpdata"
else
    echo >/dev/null
    isgreen "install ntpdata"
fi

ntpdate cn.pool.ntp.org >/dev/null 2>&1
isgreen 时间同步
if ! grep -q cn.pool.ntp.org /var/spool/cron/root; then
    echo "* */1 * * * /usr/sbin/ntpdate   cn.pool.ntp.org" >>/var/spool/cron/root
    isgreen 添加定时任务
fi
systemctl restart crond >/dev/null 2>&1
isgreen 重启crond

#安装docker
echo "安装docker"
if ! rpm -q docker-ce >/dev/null; then
    yum install -y docker-ce >/dev/null 2>&1
    isgreen "install docker"
else
    echo >/dev/null
    isgreen "install docker"
fi
systemctl enable docker --now >/dev/null 2>&1
isgreen 启动docker
if [ ! -f "/etc/docker/daemon.json" ]; then
    cat >/etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://t37y9kai.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
    isgreen 配置加速器
fi
sudo systemctl daemon-reload
sudo systemctl restart docker
isgreen 重启docker

#初始化k8s集群
echo "初始化k8s集群"
if ! rpm -q kubelet >/dev/null 2>&1; then
    yum install -y kubelet-1.23.1-0.x86_64 kubeadm-1.23.1-0.x86_64 kubectl-1.23.1-0.x86_64 >/dev/null 2>&1
    isgreen "安装kubelet kubeadm kubectl"
else
    echo >/dev/null
    isgreen "安装kubelet kubeadm kubectl"
fi
systemctl enable kubelet --now
isgreen 启动kubelet

kubeadm init --kubernetes-version=1.23.1 --apiserver-advertise-address=10.0.0.11 --image-repository "$image_repository" --pod-network-cidr="$pod_network" --ignore-preflight-errors=SystemVerification >/dev/null 2>&1
isgreen 初始化k8s

echo 'mkdir -p $HOME/.kube'
echo 'sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config'
echo 'sudo chown $(id -u):$(id -g) $HOME/.kube/config'
